/*
 * VHPanelClub.java
 * Created on 18 sept. 2009
 *
 * VirtualHockey Internet Platform
 * http://code.google.com/p/virtualhockey/
 *
 * Copyright (c) 2005-2009, Jens and Jan Janke (VirtualHockey Project)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
 */

package com.hockeo.vhweb.client.ui.panels;

import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.TabPanelEvent;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.HtmlContainer;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.hockeo.vhweb.client.VHMain;
import com.hockeo.vhweb.client.mvc.VHController;
import com.hockeo.vhweb.client.rpc.VHRpcCallback;
import com.hockeo.vhweb.client.rpc.VHRpcResultReceiver;
import com.hockeo.vhweb.client.ui.base.VHTabItem;
import com.hockeo.vhweb.client.ui.tabs.VHClubHistoryTab;
import com.hockeo.vhweb.client.ui.tabs.VHClubMatchesTab;
import com.hockeo.vhweb.client.ui.tabs.VHClubSummaryTab;
import com.hockeo.vhweb.client.utils.VHJsData;

/**
 * Base panel for the club info pages.
 *
 * @version $Id$
 * @author jjanke
 */
public class VHPanelClub extends LayoutContainer implements VHRpcResultReceiver<VHJsData>
{
  private static VHPanelClub  d_instance;
  private VHController        d_controller;
  private String              d_strClubId;
  private VHJsData            d_club;

  // UI elements
  //
  private static final String HEADER_HTML_BASE = "<div class='header'><table cellspacing='0' cellpadding='0'><tr><td style='padding-right: 4px;'><img src='ImageServlet?id={club_logo_id}'></td>"
                                                   + "<td style='padding-left: 4px; padding-right: 60px; border-left: 1px solid #8db2e3;'><img src='images/flags/32x32/{club_country}.png' title='{club_country_tooltip}'>&nbsp;"
                                                   + "<img src='images/flags/32x32/{assoc_id}.png' title='{assoc_tooltip}'>"
                                                   + "<br><span class='headerTitle'>{club_name}</span>"
                                                   + "<br><span class='headerSubTitle'>{league_name} ({league_level})</span></td>"
                                                   + "<td style='text-align: right;'><table cellspacing='2' cellpadding='2' style='border: 1px solid #8db2e3;'><tr>"
                                                   + "<td class='successCell' title='{tooltip_num_league_titles}'><img src='images/contests/league_32.png'></td>"
                                                   + "<td class='successCell' title='{tooltip_num_cup_titles}'><img src='images/contests/cup_32.png'></td>"
                                                   + "<td class='successCell' title='{tooltip_num_cl_titles}'><img src='images/contests/cl_32.png'></td>"
                                                   + "<td class='successCell' title='{tooltip_num_wl_titles}'><img src='images/contests/wl_32.png'></td></tr>"
                                                   + "<tr><td class='successCell' title='{tooltip_num_league_titles}'>{num_league_titles}</td>"
                                                   + "<td class='successCell' title='{tooltip_num_cup_titles}'>{num_cup_titles}</td>"
                                                   + "<td class='successCell' title='{tooltip_num_cl_titles}'>{num_cl_titles}</td>"
                                                   + "<td class='successCell' title='{tooltip_num_wl_titles}'>{num_wl_titles}</td>"
                                                   + "</tr></table></td></tr></table></div>";
  private HtmlContainer       d_htmlHeader;
  private TabPanel            d_tabPanel;
  private VHClubSummaryTab    d_tabItemSummary;
  private VHClubMatchesTab    d_tabItemMatches;
  private VHClubHistoryTab    d_tabItemSeasonHistory;

  /**
   * Creates a new panel to display club specific information.
   *
   * @param controller the display controller
   * @param strClubId the ID of the club for which to build this panel
   */
  private VHPanelClub( VHController controller )
  {
    d_controller = controller;

    setLayout( new BorderLayout() );
    setBorders( false );
    setAutoWidth( true );
    setLayoutOnChange( true );

    // header
    //
    d_htmlHeader = new HtmlContainer();

    BorderLayoutData data = new BorderLayoutData( LayoutRegion.NORTH );
    data.setMargins( new Margins( 4, 4, 4, 4 ) );
    data.setSize( 110f );
    add( d_htmlHeader, data );

    // tab panel
    //
    d_tabPanel = new TabPanel();
    d_tabPanel.setBorders( false );
    d_tabPanel.setAutoWidth( true );
    d_tabPanel.setTabScroll( true );

    data = new BorderLayoutData( LayoutRegion.CENTER );
    data.setMargins( new Margins( 0, 4, 4, 4 ) );
    add( d_tabPanel, data );

    // event listener to allow deferred content building for the individual tab items
    //
    d_tabPanel.addListener( Events.Select, new Listener<TabPanelEvent>() {
      public void handleEvent( TabPanelEvent evt )
      {
        ( (VHTabItem) evt.getItem() ).buildContent();
        ( (VHTabItem) evt.getItem() ).layout();
      }
    } );
  }

  /**
   * Called when a club has been loaded using RPC.
   */
  public void setRpcData( VHJsData club, String strPurpose )
  {
    loadClub( club );
  }

  /**
   * Customises the panel to display data for the given club.
   *
   * @param strClubId the ID of the club for which to display data
   */
  private void loadClub( String strClubId )
  {
    // If the panel is already configured to display data for the given club, we can skip
    // further actions
    //
    if ( d_strClubId != null && d_strClubId.equals( strClubId ) )
      return;

    // Load club data via RPC
    //
    VHRpcCallback<VHJsData> callback = new VHRpcCallback<VHJsData>( this, null );
    VHController.getBaseDataService().getClub( VHController.getLocale(), strClubId, callback );
  }

  /**
   * Loads a club that already exists as mirrored server object.
   *
   * @param club the club to be displayed
   */
  private void loadClub( VHJsData club )
  {
    d_strClubId = club.getId();
    d_club = club;

    // modify the club header
    //
    String strHtml = HEADER_HTML_BASE.replace( "{club_logo_id}", String.valueOf( d_club.getInt( "image100Id" ) ) );
    strHtml = strHtml.replace( "{club_name}", d_club.getString( "name" ) );
    strHtml = strHtml.replace( "{club_country}", d_club.getString( "country" ) );
    strHtml = strHtml.replace( "{club_country_tooltip}", VHMain.getMessages().clubHomeCountry(
        d_club.getString( "country_translated" ) ) );
    strHtml = strHtml.replace( "{assoc_id}", d_club.getString( "association_id" ) );
    strHtml = strHtml.replace( "{assoc_tooltip}", VHMain.getMessages().clubAssociation(
        d_club.getString( "association_name" ) ) );
    strHtml = strHtml.replace( "{league_name}", d_club.getString( "league_id_translated" ) );
    strHtml = strHtml.replace( "{league_level}", String.valueOf( d_club.getInt( "league_category" ) ) );
    strHtml = strHtml.replace( "{num_league_titles}", String.valueOf( d_club.getInt( "stats_numLeagueTitles" ) ) );
    strHtml = strHtml.replace( "{num_cup_titles}", String.valueOf( d_club.getInt( "stats_numCupTitles" ) ) );
    strHtml = strHtml.replace( "{num_cl_titles}", String.valueOf( d_club.getInt( "stats_numEU1Titles" ) ) );
    strHtml = strHtml.replace( "{num_wl_titles}", String.valueOf( d_club.getInt( "stats_numEU2Titles" ) ) );
    strHtml = strHtml.replace( "{tooltip_num_league_titles}", VHMain.getConstants().numLeagueTitles() );
    strHtml = strHtml.replace( "{tooltip_num_cup_titles}", VHMain.getConstants().numCupTitles() );
    strHtml = strHtml.replace( "{tooltip_num_cl_titles}", VHMain.getConstants().numEU1Titles() );
    strHtml = strHtml.replace( "{tooltip_num_wl_titles}", VHMain.getConstants().numEU2Titles() );
    d_htmlHeader.setHtml( strHtml );

    // load tabs if they are not yet loaded or modify their content if they are
    //
    if ( d_tabItemSummary == null )
    {
      d_tabItemSummary = new VHClubSummaryTab( d_controller, d_club );
      d_tabPanel.add( d_tabItemSummary );

      d_tabItemMatches = new VHClubMatchesTab( d_controller, d_club );
      d_tabPanel.add( d_tabItemMatches );

      d_tabItemSeasonHistory = new VHClubHistoryTab( d_controller, d_club );
      d_tabPanel.add( d_tabItemSeasonHistory );
    }
    else
    {
      d_tabPanel.setSelection( d_tabItemSummary );
      d_tabItemSummary.setClub( d_club );
      d_tabItemMatches.setClub( d_club );
      d_tabItemSeasonHistory.setClub( d_club );
    }
  }

  /**
   * Returns a panel to display club specific information.
   *
   * @param controller the display controller
   * @param strClubId the ID of the club for which to build this panel
   */
  public static VHPanelClub get( VHController controller, String strClubId )
  {
    if ( d_instance == null )
      d_instance = new VHPanelClub( controller );

    d_instance.loadClub( strClubId );

    return d_instance;
  }
}
